package com.greateffect.littlebud.lib.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

/**
 * Created by ZengCS on 2019/3/11.
 * function：时间转换格式
 */
public class TimeFormatUtil {
    private static SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.CHINA);
    private static SimpleDateFormat commonSimpleDateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.CHINA);

    private TimeFormatUtil() {
    }


    public static String getDateStrFromLocalDateStr(String localDataStr) {
        try {
            Date date = localSimpleDateFormat.parse(localDataStr);
            String dateStr = commonSimpleDateFormat.format(date);
            return dateStr;
        } catch (ParseException e) {
            e.printStackTrace();
            return localDataStr;
        }
    }

    public static long getDateFromDateStr(String dateStr) {
        try {
            Date date = commonSimpleDateFormat.parse(dateStr);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String getTimeStrFromDate(String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
        return format.format(new Date());
    }

    private static StringBuilder mFormatBuilder = new StringBuilder();
    private static Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

    /**
     * 时间转换 (format : 00：00)
     * @param timeMs
     * @return
     */
    public static String getTimeStrFromTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    /**
     * 时间转换 (format : 00'00'')
     * @param timeMs
     * @return
     */
    public static String getTimeStrFromTime2(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d'%02d'%02d\"", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d'%02d\"", minutes, seconds).toString();
        }
    }

    public static String getChinaTimeStrFromTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d时%02d分%02d秒", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d分%02d秒", minutes, seconds).toString();
        }
    }

    /**
     * 转换时间，舍去秒 格式：xx时xx分
     * @param timeMs 毫秒
     * @return
     */
    public static String getChinaTime2Str(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d时%02d分", hours, minutes).toString();
        } else {
            return mFormatter.format("%02d分", minutes).toString();
        }
    }
}
